/**
 * @file TaskStatAdapter.h
 * @author rj.wang (rj.wang@aotbot.com)
 * @brief 数据格式转换
 * @version 0.1
 * @date 2024-11-11
 * @copyright Copyright (c) 2024
 */

#ifndef __STATISTICS_TASK_STAT_ADAPTER__
#define __STATISTICS_TASK_STAT_ADAPTER__

#include "TaskStat.h"
#include "v3_msgs/PathGenerateManager.h"
#include "glog_func/log.h"

namespace common {
namespace statistics {

/**
 * @brief 外框类型格式转换
 * @param path_types 任务类型
 * @param area_clean 是否区域清扫
 * @return uint8_t 外框类型
 */
inline uint8_t ToBoundaryType(
    const std::vector<v3_msgs::PathGenerateType>& path_types,
    const bool area_clean) {
  uint8_t bound_type = TaskStat::NO_BOUND;
  LOG(INFO) << "path_types.size() is " << path_types.size();
  if (!area_clean) { // 示教路线
    bound_type = TaskStat::NO_BOUND;
  } else if (path_types.size() == 1) {
    LOG(INFO) << "path_types[0].type is " << (int)path_types[0].type;
    if (path_types[0].type == v3_msgs::PathGenerateType::EDGE_METHOD) { // 仅贴边
      bound_type = TaskStat::NO_BOUND;
    } else if (path_types[0].type == v3_msgs::PathGenerateType::MANUAL_POLYGON_METHOD) { // 示教区域
      bound_type = TaskStat::TEACH_BOUND;
    } else { // 手绘区域
      bound_type = TaskStat::DRAW_BOUND;
    }
  } else if (path_types.size() == 2) { // 手绘区域 (贴边 + 回字/弓字) 
    bound_type = TaskStat::DRAW_BOUND;
  }
  LOG(INFO) << "bound_type: " << (int)bound_type;
  return bound_type;
}

/**
 * @brief 外框格式转换
 * @param polygon 任务外框
 * @param area_clean 是否区域清扫
 * @return PoseVec 外框
 */
inline PoseVec ToBoundary(const std::vector<geometry_msgs::Pose>& polygon,
                          const bool area_clean) {
  PoseVec boundary{};
  if (area_clean) {
    for(const auto& pose : polygon){
      boundary.emplace_back(pose.position.x, pose.position.y);
    }
  }
  return boundary;
}

/**
 * @brief 清扫路径格式转换
 * @param paths 覆盖路径
 * @param edge_paths 贴边路径
 * @return std::vector<PoseVec> 清扫路径
 */
inline std::vector<PoseVec> ToCleanPaths(
    const std::vector<v3_msgs::TaskPath>& paths,
    const std::vector<v3_msgs::TaskPath>& edge_paths) {
  //
  std::vector<PoseVec> clean_paths{};
  for (const auto& path : paths) {
    PoseVec clean_path{};
    for (const auto& pose : path.poses) {
      clean_path.emplace_back(pose.position.x, pose.position.y);
    }
    clean_paths.emplace_back(clean_path);
  }
  for (const auto& path : edge_paths) {
    PoseVec edge_path{};
    for (const auto& pose : path.poses) {
      edge_path.emplace_back(pose.position.x, pose.position.y);
    }
    clean_paths.emplace_back(edge_path);
  }
  return clean_paths;
}

} // end of namespace common
} // end of namespace statistics

#endif